package com.hzhr.iot.web.controller;

import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.alarmData.AlarmDataModel;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataModel;
import com.hzhr.iot.device.sdk.model.collectDevice.CollectDeviceGetModel;
import com.hzhr.iot.device.sdk.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.sdk.service.Device_AlarmDataService;
import com.hzhr.iot.device.sdk.service.Device_CollectDataService;
import com.hzhr.iot.device.sdk.service.Device_CollectDeviceService;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import com.hzhr.iot.web.constant.DeviceOptions;
import com.hzhr.iot.web.controller.base.BaseController;
import com.hzhr.iot.web.model.response.BaseResponse;
import com.hzhr.iot.web.util.DateHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RequestMapping("/collectdevice")
@Api(description = "采集设备")
@RestController
public class CollectDeviceController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    Device_CollectDeviceService device_collectDeviceService;
    @Autowired
    DeviceOptions deviceOptions;
    @Autowired
    Device_CollectDataService device_collectDataService;
    @Autowired
    Device_AlarmDataService device_alarmDataService;
    @Autowired
    Device_IotDeviceService device_iotDeviceService;

    @ApiOperation(value = "获取列表", notes = "获取列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "单页面条数", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "设备ID", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "begintime", value = "安装开始时间，形如2018-01-01 23:00:00", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "endtime", value = "安装结束时间，形如2018-01-01 23:00:00", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "address", value = "安装地址", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "isvalid", value = "状态,1启用 0停用 -1全部", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "ismount", value = "挂载状态, 0未挂载 1已挂载 -1全部", required =false,dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "companyId", value = "然气公司ID", required =false,dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "deviceid", value = "设备号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "iotdeviceId", value = "所属物联设备", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "gasuserid", value = "所属燃气用户", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "挂载类型,1物联设备 2燃气用户", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "latitudeandlongitude", value = "是否有经纬度 1.有经纬度 0.全部  默认0", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "devicetype", value = "设备类型,通用字典表配置", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "userno", value = "用户号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "dept", value = "所属部门", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "prodfactory", value = "生产厂家", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "iotprodfactory", value = "物联设备生产厂家", dataType = "String", required = false, paramType = "query")
    })
    @RequestMapping(value = "/getlist", method = RequestMethod.POST)
    BaseResponse getlist(@RequestParam(value ="pageNum", required = false) Integer pageNum,
                         @RequestParam(value = "pageSize", required = false) Integer pageSize,
                         @RequestParam(value = "uat", required = true) String uat,
                         @RequestParam(value = "id", required = false) String id,
                         @RequestParam(value = "begintime", required = false) String begintime,
                         @RequestParam(value = "endtime", required = false) String endtime,
                         @RequestParam(value = "address", required = false) String address,
                         @RequestParam(value = "isvalid", required = false) Integer isvalid,
                         @RequestParam(value = "ismount", required = false) Integer ismount,
                         @RequestParam(value = "companyId", required = false) String companyId,
                         @RequestParam(value = "deviceid", required = false) String deviceid,
                         @RequestParam(value = "iotdeviceId", required = false) String iotdeviceId,
                         @RequestParam(value = "gasuserid", required = false) String gasuserid,
                         @RequestParam(value = "type", required = false) Integer type,
                         @RequestParam(value = "latitudeandlongitude", required = false) Integer latitudeandlongitude,
                         @RequestParam(value = "devicetype", required = false) String devicetype,
                         @RequestParam(value = "userno", required = false) String userno,
                         @RequestParam(value = "username", required = false) String username,
                         @RequestParam(value = "dept", required = false) String dept,
                         @RequestParam(value = "prodfactory", required = false) String prodfactory,
                         @RequestParam(value = "iotprodfactory", required = false) String iotprodfactory) throws Exception {
        String token = getToken();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, Object> query = new HashMap<>();
        query.put("show", "list");
        query.put("page", pageNum);
        query.put("count", pageSize);
        query.put("show_info", true);
        query.put("show_type", true);
        query.put("show_gasUser", true);
        query.put("show_iotDevice", true);
        query.put("show_dept", true);
        query.put("show_total", true);
        if (!StringUtils.isEmpty(deviceid)) {
            query.put("like_code", deviceid + "%");
        }
        if (!StringUtils.isEmpty(iotdeviceId)) {
            query.put("like_iotDeviceCode", iotdeviceId + "%");
        }
        if (!StringUtils.isEmpty(userno)) {
            query.put("like_gasUserCode", userno + "%");
        }
        if (!StringUtils.isEmpty(username)) {
            query.put("like_gasUserName", "%" + username + "%");
        }
        if (!StringUtils.isEmpty(address)) {
            query.put("like_gasUserAddress", "%" + address + "%");
        }
        if (!StringUtils.isEmpty(dept)) {
            query.put("like_deptCode", dept + "%");
        }
        if (!StringUtils.isEmpty(begintime)) {
            query.put("greaterthanorequal_createTime", HzhrDateUtil.strToDate(begintime).getTime());
        }
        if (!StringUtils.isEmpty(endtime)) {
            query.put("lessthan_createTime", HzhrDateUtil.strToDate(endtime).getTime());
        }
        if (!StringUtils.isEmpty(iotprodfactory)) {
            query.put("equal_iotDeviceCompanyId", iotprodfactory);
        }
        if (!StringUtils.isEmpty(prodfactory)) {
            query.put("equal_prodFactoryIotCompanyId", prodfactory);
        }
        if ("201".equals(devicetype)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "NB_IOT_GAS_METER"));
        } else if ("204".equals(devicetype)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "FLOW_METER"));
        } else if ("205".equals(devicetype)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "GAS_PRESSURE_MONITOR"));
        } else if ("206".equals(devicetype)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "BK_METER"));
        } else if ("202".equals(devicetype)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "GPRS_IOT_GAS_METER"));
        } else if ("203".equals(devicetype)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "LORA_METER"));
        }

        CollectDeviceGetModel collectDeviceGetModel = device_collectDeviceService.get(header, query);
        List<CollectDeviceModel> collectDeviceModels = collectDeviceGetModel.getModelList();
        int total = collectDeviceGetModel.getTotal();

        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> dataList = new ArrayList<>();
        for (CollectDeviceModel collectDeviceModel :
                collectDeviceModels) {
            Map<String, Object> dataItem = new HashMap<>();
            dataItem.put("id", HzhrTypeUtil.object2string(collectDeviceModel.getId()));
            dataItem.put("deviceid", HzhrTypeUtil.object2string(collectDeviceModel.getCode()));
            dataItem.put("gasuserId", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserId()));
            dataItem.put("iotdeviceId", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceId()));
            dataItem.put("deptId", HzhrTypeUtil.object2string(collectDeviceModel.getDeptId()));
            dataItem.put("model", HzhrTypeUtil.object2string(collectDeviceModel.getTypeDes()));
            dataItem.put("des", HzhrTypeUtil.object2string(collectDeviceModel.getDes()));
            dataItem.put("address", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserAddress()));
            dataItem.put("gascompanyId", HzhrTypeUtil.object2string(collectDeviceModel.getCompanyId()));
            dataItem.put("installtime", HzhrTypeUtil.object2string(collectDeviceModel.getCreateTime()));
            dataItem.put("createtime", HzhrTypeUtil.object2string(collectDeviceModel.getCreateTime()));
            dataItem.put("lastreadingtime", HzhrTypeUtil.object2string(collectDeviceModel.getLastCollectTime()));
            dataItem.put("lastvctime", HzhrTypeUtil.object2string(collectDeviceModel.getLastCollectTime()));
            dataItem.put("prodfactory", HzhrTypeUtil.object2string(collectDeviceModel.getProdFactoryDes()));
            Double currentReading = collectDeviceModel.getCurrentReading();
            dataItem.put("gasconsume", currentReading != null ? new BigDecimal(currentReading).setScale(3, BigDecimal.ROUND_DOWN).toString() : null);
            dataItem.put("longitude", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceLongitude()));
            dataItem.put("latitude", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceLatitude()));

            String categoryCode = collectDeviceModel.getCategoryCode();
            if (this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "NB_IOT_GAS_METER").equals(categoryCode)) {
                dataItem.put("type", "201");
            } else if (this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "GPRS_IOT_GAS_METER").equals(categoryCode)) {
                dataItem.put("type", "202");
            } else if (this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "LORA_METER").equals(categoryCode)) {
                dataItem.put("type", "203");
            } else if (this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "BK_METER").equals(categoryCode)) {
                dataItem.put("type", "205");
            } else if (this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "FLOW_METER").equals(categoryCode)) {
                dataItem.put("type", "204");
            } else if (this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "GAS_PRESSURE_MONITOR").equals(categoryCode)) {
                dataItem.put("type", "206");
            }

            Map<String, Object> deviceGasuser_gasuserId = new HashMap<>();
            deviceGasuser_gasuserId.put("no", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserCode()));
            deviceGasuser_gasuserId.put("name", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserName()));
            deviceGasuser_gasuserId.put("address", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserAddress()));
            dataItem.put("deviceGasuser_gasuserId", deviceGasuser_gasuserId);

            Map<String, Object> deviceIotdevice_iotdeviceId = new HashMap<>();
            deviceIotdevice_iotdeviceId.put("id", HzhrTypeUtil.object2string(collectDeviceModel.getId()));
            deviceIotdevice_iotdeviceId.put("deviceid", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceCode()));
            deviceIotdevice_iotdeviceId.put("des", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceDes()));
            deviceIotdevice_iotdeviceId.put("model", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceTypeDes()));
            deviceIotdevice_iotdeviceId.put("sim", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceSimNo()));
            deviceIotdevice_iotdeviceId.put("simtype", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceSimTypeDes()));
            deviceIotdevice_iotdeviceId.put("longitude", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceLongitude()));
            deviceIotdevice_iotdeviceId.put("latitude", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceLatitude()));
            dataItem.put("deviceIotdevice_iotdeviceId", deviceIotdevice_iotdeviceId);

            Map<String, Object> options_valvestatus = new HashMap<>();
            options_valvestatus.put("name", HzhrTypeUtil.object2string(collectDeviceModel.getCurrentValveStatusDes()));
            dataItem.put("options_valvestatus", options_valvestatus);

            Map<String, Object> userDept_deptId = new HashMap<>();
            userDept_deptId.put("name", collectDeviceModel.getManagementName());
            userDept_deptId.put("parentname", collectDeviceModel.getSubCompanyName());
            dataItem.put("userDept_deptId", userDept_deptId);

            dataItem.put("isvalid", "1");
            dataList.add(dataItem);
        }
        result.put("dataList", dataList);
        result.put("total", total);

        return new BaseResponse("0", "成功", result);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    BaseResponse info(@RequestParam(value = "uat", required = true) String uat,
                      @RequestParam(value = "id", required = true,defaultValue = "0") String id) throws Exception {
        //查询采集设备
        String token = getToken();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, Object> query = new HashMap<>();
        query.put("show", "one");
        query.put("show_info", true);
        query.put("show_type", true);
        query.put("show_gasUser", true);
        query.put("show_iotDevice", true);
        query.put("show_total", true);
        query.put("show_dept", true);
        query.put("equal_id", id);
        CollectDeviceModel collectDeviceModel = device_collectDeviceService.get(header, query).getModel();
        if (collectDeviceModel == null) return new BaseResponse("0", "成功", new HashMap<>());

        Map<String, Object> result = new HashMap<>();
        result.put("id", HzhrTypeUtil.object2string(collectDeviceModel.getId()));
        result.put("deviceid", HzhrTypeUtil.object2string(collectDeviceModel.getCode()));
        result.put("gasuserId", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserId()));
        result.put("iotdeviceId", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceId()));
        result.put("deptId", HzhrTypeUtil.object2string(collectDeviceModel.getDeptId()));
        result.put("model", HzhrTypeUtil.object2string(collectDeviceModel.getTypeDes()));
        result.put("des", HzhrTypeUtil.object2string(collectDeviceModel.getDes()));
        result.put("address", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserAddress()));
        result.put("gascompanyId", HzhrTypeUtil.object2string(collectDeviceModel.getCompanyId()));
        result.put("installtime", HzhrTypeUtil.object2string(collectDeviceModel.getCreateTime()));
        result.put("createtime", HzhrTypeUtil.object2string(collectDeviceModel.getCreateTime()));
        result.put("lastreadingtime", HzhrTypeUtil.object2string(collectDeviceModel.getLastCollectTime()));
        result.put("lastvctime", HzhrTypeUtil.object2string(collectDeviceModel.getLastCollectTime()));
        result.put("pressureunit", HzhrTypeUtil.object2string(collectDeviceModel.getPressureUnitDes()));
        result.put("longitude", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceLongitude()));
        result.put("latitude", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceLatitude()));

        if (deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "NB_IOT_GAS_METER").equals(collectDeviceModel.getCategoryCode())
                || deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "GPRS_IOT_GAS_METER").equals(collectDeviceModel.getCategoryCode())) {
            result.put("prodfactory", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceCompanyName()));
        } else {
            result.put("prodfactory", HzhrTypeUtil.object2string(collectDeviceModel.getProdFactoryDes()));
        }

        Double currentReading = collectDeviceModel.getCurrentReading();
        result.put("gasconsume", currentReading != null ? new BigDecimal(currentReading).setScale(3, BigDecimal.ROUND_DOWN).toString() : null);

        Map<String, Object> deviceGasuser_gasuserId = new HashMap<>();
        deviceGasuser_gasuserId.put("no", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserCode()));
        deviceGasuser_gasuserId.put("name", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserName()));
        deviceGasuser_gasuserId.put("address", HzhrTypeUtil.object2string(collectDeviceModel.getGasUserAddress()));
        result.put("deviceGasuser_gasuserId", deviceGasuser_gasuserId);

        Map<String, Object> deviceIotdevice_iotdeviceId = new HashMap<>();
        deviceIotdevice_iotdeviceId.put("deviceid", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceCode()));
        deviceIotdevice_iotdeviceId.put("des", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceDes()));
        deviceIotdevice_iotdeviceId.put("model", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceTypeDes()));
        deviceIotdevice_iotdeviceId.put("sim", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceSimNo()));
        deviceIotdevice_iotdeviceId.put("simtype", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceSimTypeDes()));
        deviceIotdevice_iotdeviceId.put("prodfactory", HzhrTypeUtil.object2string(collectDeviceModel.getIotDeviceCompanyName()));
        result.put("deviceIotdevice_iotdeviceId", deviceIotdevice_iotdeviceId);

        Map<String, Object> options_valvestatus = new HashMap<>();
        options_valvestatus.put("name", HzhrTypeUtil.object2string(HzhrTypeUtil.object2string(collectDeviceModel.getCurrentValveStatusDes())));
        result.put("options_valvestatus", options_valvestatus);

        Map<String, Object> userDept_deptId = new HashMap<>();
        userDept_deptId.put("name", collectDeviceModel.getManagementName());
        userDept_deptId.put("parentname", collectDeviceModel.getSubCompanyName());
        result.put("userDept_deptId", userDept_deptId);

        //查询采集数据
        query = new HashMap<>();
        query.put("show", "one");
        query.put("show_info", true);
        query.put("equal_collectDeviceId", collectDeviceModel.getId());
        query.put("orderBy_currentTm", "desc");
        CollectDataModel collectDataModel = device_collectDataService.get(header, query).getModel();
        if (collectDataModel != null) {
            Map<String, Object> collectData = new HashMap<>();
            collectData.put("currenttime",HzhrTypeUtil.object2string(collectDataModel.getCurrentTm()));
            Double currentValue = collectDataModel.getCurrentValue();
            collectData.put("currentvalue", currentValue != null ? new BigDecimal(currentValue).setScale(3, BigDecimal.ROUND_DOWN).toString() : null);
            collectData.put("cellvoltage",HzhrTypeUtil.object2string(collectDataModel.getCellVoltage()));
            Map<String, String> options_valvestatus1 = new HashMap<>();
            options_valvestatus1.put("remarks", HzhrTypeUtil.object2string(collectDataModel.getValveStatusDes()));
            collectData.put("options_valvestatus", options_valvestatus1);
            collectData.put("pressure", HzhrTypeUtil.object2string(collectDataModel.getPressure()));
            collectData.put("temperature", HzhrTypeUtil.object2string(collectDataModel.getTemperature()));
            result.put("collectData", collectData);
        }

        //查询报警数据
        if (deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "GAS_PRESSURE_MONITOR").equals(collectDeviceModel.getCategoryCode())) {
            query = new HashMap<>();
            query.put("show", "one");
            query.put("show_info", true);
            query.put("equal_collectDeviceId", collectDeviceModel.getId());
            query.put("isnull_alarmCodes", false);
            query.put("orderBy_currentTm", "desc");
            collectDataModel = device_collectDataService.get(header, query).getModel();
            if (collectDataModel != null) {
                Map<String, Object> alarmdata = new HashMap<>();
                String currentTm = HzhrTypeUtil.object2string(collectDataModel.getCurrentTm());
                if (!StringUtils.isEmpty(currentTm)) {
                    alarmdata.put("currenttime", HzhrDateUtil.dateToStr(new Date(Long.parseLong(currentTm))));
                }

                Map<String, String> options_alarmtype = new HashMap<>();
                String alarmCodes = collectDataModel.getAlarmCodes();
                if (!StringUtils.isEmpty(alarmCodes)) {
                    List<String> alarmCodesList = Arrays.asList(alarmCodes.toString().split(","));
                    String alarmStatusName = "";
                    for (int i = 0; i < alarmCodesList.size(); i++) {
                        String alarmCodesItem = alarmCodesList.get(i);
                        String alarmStatusNameItem = deviceOptions.getDesByCode("ALARM_TYPE", alarmCodesItem);
                        if (!StringUtils.isEmpty(alarmStatusNameItem)) {
                            if (i == alarmCodesList.size() - 1) {
                                alarmStatusName += alarmStatusNameItem;
                            } else {
                                alarmStatusName += alarmStatusNameItem + ", ";
                            }
                        }
                    }
                    options_alarmtype.put("remarks", alarmStatusName);
                }
                alarmdata.put("options_alarmtype", options_alarmtype);
                result.put("alarmdata", alarmdata);
            }
        } else {
            query = new HashMap<>();
            query.put("show", "one");
            query.put("show_info", true);
            query.put("equal_collectDeviceId", collectDeviceModel.getId());
            query.put("orderBy_currentTm", "desc");
            AlarmDataModel alarmDataModel = device_alarmDataService.get(header, query).getModel();
            if (alarmDataModel != null) {
                Map<String, Object> alarmdata = new HashMap<>();
                alarmdata.put("currenttime",HzhrTypeUtil.object2string(alarmDataModel.getCurrentTm()));
                Map<String, String> options_alarmtype = new HashMap<>();
                options_alarmtype.put("remarks", HzhrTypeUtil.object2string(alarmDataModel.getAlarmTypeDes()));
                alarmdata.put("options_alarmtype", options_alarmtype);
                result.put("alarmdata", alarmdata);
            }
        }

        result.put("isvalid", "1");

        return new BaseResponse("0", "成功", result);

    }

    @ApiOperation(value = "列表页顶部统计数据", notes = "列表页顶部统计数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "gascompanyid", value = "然气公司ID", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "type", value = "设备类型,通用字典表配置", dataType = "String", required = false, paramType = "query")
    })
    @RequestMapping(value = "/listtopcount", method = RequestMethod.POST)
    BaseResponse listtopcount(@RequestParam(value = "uat", required = true) String uat,
                              @RequestParam(value = "gascompanyid", required = false,defaultValue = "0") String gascompanyid,
                              @RequestParam(value = "type", required = false,defaultValue = "") String type) throws Exception {
        String token = getToken();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);
        Map<String, Object> query = new HashMap<>();
        query.put("show_total", true);

        if ("201".equals(type)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "NB_IOT_GAS_METER"));
        } else if ("204".equals(type)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "FLOW_METER"));
        } else if ("205".equals(type)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "GAS_PRESSURE_MONITOR"));
        } else if ("206".equals(type)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "BK_METER"));
        } else if ("202".equals(type)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "GPRS_IOT_GAS_METER"));
        } else if ("203".equals(type)) {
            query.put("equal_categoryCode", this.deviceOptions.getCode("COLLECT_DEVICE_CATEGORY", "LORA_METER"));
        }

        query.put("greaterthanorequal_createTime", Long.valueOf(DateHelper.getTimesWeekmorning().getTime()));
        int weekAddCount = this.device_collectDeviceService.get(header, query).getTotal().intValue();
        query.put("greaterthanorequal_createTime", Long.valueOf(DateHelper.getDayBeginTime(new Date()).getTime()));
        int yesterdayAddCount = this.device_collectDeviceService.get(header, query).getTotal().intValue();
        query.put("greaterthanorequal_createTime", Long.valueOf(DateHelper.getMonthBeginTime(new Date()).getTime()));
        int monthAddCount = this.device_collectDeviceService.get(header, query).getTotal().intValue();
        query.put("greaterthanorequal_createTime", Long.valueOf(DateHelper.getCurrYearFirst().getTime()));
        int yearAddCount = this.device_collectDeviceService.get(header, query).getTotal().intValue();
        query.put("greaterthanorequal_createTime", null);
        int deviceCount = this.device_collectDeviceService.get(header, query).getTotal().intValue();


        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("WeekCollectDeviceAddCount", Integer.valueOf(weekAddCount));
        map.put("YesterdayCollectDeviceAddCount", Integer.valueOf(yesterdayAddCount));
        map.put("MonthCollectDeviceAddCount", Integer.valueOf(monthAddCount));
        map.put("YearCollectDeviceAddCount", Integer.valueOf(yearAddCount));
        map.put("CollectDeviceCount", Integer.valueOf(deviceCount));

        return new BaseResponse("0", "成功",map);
    }
}
